package com.ogawa.chair7808.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.ogawa.chair7808.R;
import com.ogawa.chair7808.adapter.AutoProgramAdapter;
import com.ogawa.chair7808.cache.GlobalCache;
import com.ogawa.chair7808.manager.DataManager;
import com.ogawa.chair7808.view.AutoProgramBean;

import java.util.ArrayList;
import java.util.List;

import static com.ogawa.base.Constant.Constant.LIVE_EVENT_BUS_KEY_PROF_MASS;

/**
 *主题模式
 */

public class Layout7808Ztms extends RelativeLayout{
    RecyclerView recyclerView;
    AutoProgramAdapter adapter = new AutoProgramAdapter();
    public Layout7808Ztms(Context context) {
        super(context);
        init();
    }

    public Layout7808Ztms(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    void init(){
        LayoutInflater.from(getContext()).inflate(R.layout.layout_auto_program, this);
        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        List<AutoProgramBean> autoProgramBeanList = new ArrayList<>();
        autoProgramBeanList.add(new AutoProgramBean(getContext().getString(R.string.zy_masa_sbz),R.drawable.zymasa_sbz_seletor,"9","9"));
        autoProgramBeanList.add(new AutoProgramBean(getContext().getString(R.string.zy_masa_dtz),R.drawable.zymasa_low_seletor,"10","10"));
        autoProgramBeanList.add(new AutoProgramBean(getContext().getString(R.string.zy_masa_jcz),R.drawable.zymasa_jcz_seletor,"11","11"));
        autoProgramBeanList.add(new AutoProgramBean(getContext().getString(R.string.zy_masa_shoes),R.drawable.zymasa_ggz_seletor,"12","12"));
        adapter.setNewInstance(autoProgramBeanList);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                AutoProgramBean bean = (AutoProgramBean) adapter.getData().get(position);
                LiveEventBus.get(LIVE_EVENT_BUS_KEY_PROF_MASS, AutoProgramBean.class).post(bean);
                LogUtils.e("点击：" + bean.getName());
//                DataManager.getDataManager().sendCommond("autoProgram",bean.getSendcCode());
            }
        });
        recyclerView.setAdapter(adapter);
    }

    public void setSelect() {
        try{
            String modelValue = GlobalCache.getDeviceStatusBean().getAutoProgram();
            if (modelValue == null){
                return;
            }
            for (int i = 0; i < adapter.getData().size(); i++) {
                if (modelValue.equals(adapter.getData().get(i).getReceiveCode())) {
                    adapter.getData().get(i).setSelect(true);
                } else {
                    adapter.getData().get(i).setSelect(false);
                }
            }
            adapter.notifyDataSetChanged();
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
